<?php if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' ); ?>

<?php
/**
 * This file displays the part payment option shown in the products.
 */
include_once(CLASSPATH . '/payment/ps_kreditor_konto.cfg.php');
$dbk = new ps_DB;
$qk = "SELECT payment_enabled FROM #__{vm}_payment_method WHERE payment_class = 'ps_kreditor_konto'";
$dbk->query($qk);
if (KREDITOR_KONTO_DISPLAY_KLARNA_PRICE == 'Y' && $dbk->f('payment_enabled') == 'Y') {
	include_once(CLASSPATH . '/payment/ps_kreditor_language.php');

	$dbCountrie = new ps_DB;
	$q  = "SELECT * from #__{vm}_user_info WHERE user_id='" . $_SESSION['auth']['user_id'] . "' AND address_type='BT'";
	$dbCountrie->query($q);
	$country = $dbCountrie->f('country');

	//If not logged on: continue.
	//If logged on and from valid countrie: continue.
	//Else: move along elsewhere. Nothing will be printed.
	if(!isset($country) || in_array($country, getValidCountrieCodes())) {
		$joomlaLang = getJoomlaOrElseVendorLanguage();
		//If not logged on: then check if joomla language(if not joomla valid lang, then vendor lang)
		//and if that is valid then continue.
		//ElseIf: logged on then above statement takes care of all.
		//Else: move along elsewhere. Nothing will be printed.
		if(!isset($country) && in_array($joomlaLang, getValidCountrieCodes())) {
			$country = $joomlaLang;
		}
		
		/**
		 * If a countrie is set(and hence valid), then continue, else just move along...
		 */
		if(isset($country)) {
			include_once('util.php');
			include_once(CLASSPATH . '/payment/ps_kreditor.cfg.php');

			$temp = $CURRENCY_DISPLAY->getFullValue($base_price);
			$current_currency;			
			if($_SESSION['product_currency'] == $_SESSION['vendor_currency']) {
				$temp = $_SESSION[product_currency];
			}
			if(strstr($temp, "SEK")) {
				$current_currency = 'KRED_SEK';
			} else if(strstr($temp, "NOK")) {
				$current_currency = 'KRED_NOK';
			} else if(strstr($temp, "DKK")) {
				$current_currency = 'KRED_DKK';
			} else if(strstr($temp, "EUR")) {
				$current_currency = 'KRED_EUR';
			} else {
				if(isset($_SESSION['product_currency'])) {
					$current_currency = 'KRED_' . $_SESSION['product_currency'];
				} else {
					$current_currency = 'KRED_' . $_SESSION['vendor_currency'];
				}
			}

			/**
			 * If error searching the currency use this to check that the given currency is the same as
			 * the below commented echo.
			 */
			//echo '<br />' . $temp . '<br />' . $current_currency . '<br />';
			
			$countryNumber = 0;
			switch($country) {
				case "SWE": $countryNumber = 209; break;
				case "NOR": $countryNumber = 164; break;
				case "DNK": $countryNumber = 59; break;
				case "DEU": $countryNumber = 81; break;
				case "FIN": $countryNumber = 73; break;
				case "NLD": $countryNumber = 154; break;
			}
			
			
			activateLanguage();
			$langInfo = new Singleton("language");
//			$country = $langInfo->Country;
//			if(empty($country)) {
//				$db2 = new ps_DB;
//				$q2 = "SELECT vendor_country FROM #__{vm}_vendor WHERE vendor_id = '" . $_SESSION['ps_vendor_id'] . "';";
//				$db2->query($q2);
//				$country = $db2->f("vendor_country");
//			}

			$lowest = 1000000;
//			if($current_currency == 'KRED_EUR' && strtolower($country) == "deu") {
//				$db = new ps_DB;
//				$q = "SELECT * FROM #__{vm}_kreditor_pclass WHERE interest = '1495' AND description = '1/24 avbetalningskonto' AND currency = 'KRED_EUR' ORDER BY months DESC";
//				$db->query($q);
//				if($db->next_record()) {
//					$monthly_payment = calc_monthly_product_cost($base_price*100, $db->f("interest"), $db->f("months"), $db->f("handlingfee"));
//					$lowest = min($lowest, $monthly_payment);
//				}
//			} else {
				//echo 'TEST: ' . $countryNumber . '<br>';
				$db = new ps_DB;
				//REMOVE "AND description = '1/24 avbetalningskonto'" from the query to make it use all available pclasses instead.
				$q = "SELECT * FROM #__{vm}_kreditor_pclass WHERE country = $countryNumber ORDER BY months DESC";
				
				$db->query($q);
				while($db->next_record()) {
//					if($db->f("interest") == '1495' && $db->f("description") == '1/24 avbetalningskonto' && $db->f("currency") == 'KRED_EUR') {
//						continue;
//					}
					
					$monthly_payment = calc_monthly_product_cost($base_price*100, $db->f("interest"), $db->f("months"), $db->f("handlingfee"));
					$lowest = min($lowest, $monthly_payment);
				}
//			}
			$id = 'klarna_partpayment_' . rand(1, 15000);
			if($lowest != 1000000) {
				echo '<br />' . $langInfo->TEXT_FROM . ': ' . '<span style="color: red;" class="klarna_pp_color">' . $GLOBALS['CURRENCY_DISPLAY2']->getFullValue($lowest) . '</span>' . ' / ' . $langInfo->TEXT_SHORT_MONTH . '<br />';
				?>
				<script type="text/javascript" src="http://integration.klarna.com/js/klarnapart.js"></script>
				<script>
					addKlarnaPartPaymentEvent(function() { InitKlarnaPartPaymentElements('<?php echo $id; ?>', '<?php echo KREDITOR_EID; ?>', '<?php echo strtolower($country); ?>'); });
				</script>
				<a href="#" id="<?php echo $id; ?>" onclick="ShowKlarnaPartPaymentPopup(event);return false;"></a>
		<?php }
		}
	}
}?>